/**
 * Created by Administrator on 2017/4/17.
 */
//处理删除信息
function delete_data(id, urls) {
    $.ajax({
        type: "POST",
        url: urls,
        data: "id=" + id + "&which_DB=" + document.getElementById("which_DB").value,
        success: function (msg) {
            var s = JSON.parse(msg);
            if (s.result == "删除成功") {
                window.location.reload();
            } else if (s.result == "不存在") {
                window.alert("数据不存在");
            } else if (s.error == "删除失败") {
                window.alert("删除失败");
            }
        }
    });
}
//判断有没有编辑修改信息
function change_img() {
    document.getElementById('has_img').value = "has_change";
}
//编辑信息
function update_data(id, urls) {

    var form_prompt = document.getElementById("prompt");
    $.ajax({
        type: "POST",
        url: urls,
        data: "id=" + id + "&which_DB=" + document.getElementById("which_DB").value,
        success: function (msg) {
            var json = JSON.parse(msg);
            if (json.errer == "1") {
                window.alert("没有编辑信息");
            } else {
                document.getElementById('title').value = json.title;
                var goods = document.getElementById('goodsCode');
                goods != null ? goods.value = json.goodsCode :  null;
                var img = document.getElementById('image');
                if (img != null) {
                    img.src = json.image;
                }
                document.getElementById('hidden_id').value = json.id;
                form_prompt.style.display = "block";
            }
        }
    });
}
function send_data(urls, operation) {
    var title_name;
    var goodsCode_name;
    var hidden_id_name;
    var has_img_name;
    var photo_name;
    if (operation == "update") {
        title_name = document.getElementById('title').value;
        var goods = document.getElementById('goodsCode');
        goods == null ? goodsCode_name = 'null' : goodsCode_name = goods.value;
        photo_name = "photo";
        hidden_id_name = document.getElementById('hidden_id').value;
        has_img_name = document.getElementById('has_img').value;
    } else if (operation == "add") {
        title_name = document.getElementById('title_add').value;
        goodsCode_name = document.getElementById('goodsCode_add').value;
        photo_name = "photo_add";
    }
    $.ajaxFileUpload({
        type: "POST",
        url: urls,
        data: {
            "title": title_name,
            "goodsCode": goodsCode_name,
            "hidden_id": hidden_id_name,
            "has_img": has_img_name,
            "operation": operation,
            "which_DB": document.getElementById("which_DB").value,
        },
        fileElementId: photo_name,//文件选择框的id属性
        dataType: 'json',//服务器返回的格式
        success: function (data, status) {
            if (data.result == "成功") {
                window.location.reload();
            } else if (data.result == "失败") {
                window.alert("失败");
            } else if (data.error == "数据添加失败") {
                window.alert("数据添加失败");
            }

        },
        error: function (data, status, e) {
            console.log(data.responseText);
        }
    });
    return false;
}
function insert_data() {
    var insert = document.getElementById('insert_data').style.display;
    if (insert == "none") {
        document.getElementById('insert_data').style.display = "block";
    } else if (insert == "block") {
        document.getElementById('insert_data').style.display = "none"
    }
}
//判断是否超过5条数据
function is_over(number) {
    if (parseInt(number) >= 5) {
        window.alert("添加数据不能超过5条");
    } else {
        insert_data();
    }
}
function send_data_best(urls, operation) {
    var title_name;
    var goodsCode_name;
    var hidden_id_name;
    if (operation == "update") {
        title_name = document.getElementById('title').value;
        goodsCode_name = document.getElementById('goodsCode').value;
        hidden_id_name = document.getElementById('hidden_id').value;
    } else if (operation == "add") {
        title_name = document.getElementById('title_add').value;
        goodsCode_name = document.getElementById('goodsCode_add').value;
    }
    $.ajax({
        type: "POST",
        url: urls,
        data: "hidden_id=" + hidden_id_name + "&title=" + title_name + "&goodsCode=" + goodsCode_name + "&which_DB=" + document.getElementById("which_DB").value + "&operation=" + operation,
        success: function (msg) {
            var json = JSON.parse(msg);
            if (json.errer == "数据添加失败") {
                window.alert("数据添加失败");
            } else if (json.result == "成功") {
                window.location.reload();
            } else if (json.result == "失败") {
                window.alert("失败");
            }
        }
    });
    return false;
}